package com.smm.web.vmc.controller.admin;

import com.smm.web.vmc.domain.*;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.dto.ReceiptDto;
import com.smm.web.vmc.service.CameraService;
import com.smm.web.vmc.service.ReceiptService;
import com.smm.web.vmc.service.TagService;
import com.smm.web.vmc.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/7.
 *
 */
@Slf4j
@Controller
@RequestMapping("/admin/receipt")
public class ReceiptController {


    @Resource
    private WarehouseService warehouseService;

    @Resource
    private ReceiptService receiptService;

    @Resource
    private CameraService cameraService;

    @Resource
    private TagService tagService;


    //按仓单编号搜索
    @RequestMapping("/search.do")
    public ModelAndView search(HttpServletRequest request,String no){

        int pageNo = 1;

        if(request.getParameter("page")!=null){

            pageNo = Integer.parseInt(request.getParameter("page"));
        }

        Pager pager = new Pager(pageNo);

        ModelAndView mv = new ModelAndView("admin/receipt/search");

        //获取所有仓单列表
        List<ReceiptDto> receiptList = receiptService.searchAllDtoByNo(no,pager);

        mv.addObject("receiptList",receiptList);
        mv.addObject("pager",pager);
        mv.addObject("no",no);

        return mv;

    }



    //所有仓单列表， 分页
    @RequestMapping("/list.do")
    public ModelAndView list(HttpServletRequest request){

        int pageNo = 1;

        if(request.getParameter("page")!=null){

            pageNo = Integer.parseInt(request.getParameter("page"));
        }

        Pager pager = new Pager(pageNo);

        ModelAndView mv = new ModelAndView("admin/receipt/list");

        //获取所有仓单列表
        List<ReceiptDto> receiptList = receiptService.getAllDto(pager);

        mv.addObject("receiptList",receiptList);
        mv.addObject("pager",pager);

        return mv;

    }


    //所有无效仓单
    @RequestMapping("/closedList.do")
    public ModelAndView closedList(){

        ModelAndView mv = new ModelAndView("admin/receipt/closedList");

        List<ReceiptClose> receiptList = receiptService.getAllClosedReceipts();

        mv.addObject("receiptList",receiptList);

        return mv;
    }



    @RequestMapping("/listByWh.do")
    public ModelAndView listByWh(int whId){

        ModelAndView mv = new ModelAndView("admin/receipt/listByWh");

        Warehouse warehouse = warehouseService.getOneById(whId);

        //获取指定仓库下所有仓单列表
        List<ReceiptDto> receiptList = receiptService.getAllDtoByWarehouser(whId);

        mv.addObject("receiptList",receiptList);

        mv.addObject("warehouse",warehouse);

        return mv;

    }


    @RequestMapping("/add.do")
    public ModelAndView add(int whId){

        ModelAndView mv = new ModelAndView("admin/receipt/add");

        Warehouse warehouse = warehouseService.getOneById(whId);

        //获取该仓库所有摄像头列表
        List<Camera> cameraList = cameraService.getAllByWarehouse(whId);

        //获取该仓库所有标签列表,排除已关联其他仓单的标签
        List<Tag> tagList = tagService.getAllByWarehouseAndNoRep(whId);

        mv.addObject("warehouse",warehouse);
        mv.addObject("cameraList",cameraList);
        mv.addObject("tagList",tagList);

        return mv;
    }


    @RequestMapping("/submitAdd.do")
    @ResponseBody
    public JsonReturn submitAdd(ReceiptDto receiptDto){

        log.info(String.valueOf(receiptDto));

        try{

            receiptService.add(receiptDto);

            return new JsonReturn(1,"新增成功");

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"新增失败！"+e.getMessage());
        }

    }



    @RequestMapping("/update.do")
    public ModelAndView update(int whId,String repNo){

        ModelAndView mv = new ModelAndView("admin/receipt/update");

        //获取仓单信息
        Receipt receipt = receiptService.getOneById(repNo);

        //获取该仓单关联的摄像头信息
        List<Camera> relationCameraList = cameraService.getAllByReceipt(repNo);

        //获取该仓单所有标签列表
        List<Tag> relationTagList = tagService.getAllByReceipt(repNo);

        //获取仓库信息
        Warehouse warehouse = warehouseService.getOneById(whId);

        //获取该仓库所有摄像头列表
        List<Camera> cameraList = cameraService.getAllByWarehouse(whId);

        //获取该仓库所有标签列表，排除已关联其他仓单的标签
        List<Tag> tagList = tagService.getAllByWarehouseAndNotThisRep(whId,repNo);

        mv.addObject("warehouse",warehouse);
        mv.addObject("receipt",receipt);
        mv.addObject("cameraList",cameraList);
        mv.addObject("tagList",tagList);

        mv.addObject("relationCameraList",relationCameraList);
        mv.addObject("relationTagList",relationTagList);

        return mv;
    }



    @RequestMapping("/submitUpdate.do")
    @ResponseBody
    public JsonReturn submitUpdate(ReceiptDto receiptDto){

        log.info(String.valueOf(receiptDto));

        try{

            receiptService.update(receiptDto);

            return new JsonReturn(1,"修改成功");

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"修改失败！"+e.getMessage());
        }

    }


    @RequestMapping("/del.do")
    @ResponseBody
    public JsonReturn del(String repNo){

        try{

            receiptService.delete(repNo);

            return new JsonReturn(1,"删除成功");

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"删除失败！"+e.getMessage());
        }

    }


    @RequestMapping("/close.do")
    @ResponseBody
    public JsonReturn close(String repNo){

        try{

            receiptService.close(repNo);

            return new JsonReturn(1,"关闭成功");

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,"关闭失败！"+e.getMessage());
        }

    }





}
